package com.xlf.service.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xlf.common.core.pojo.vo.UserVO;
import com.xlf.pojo.system.SysUser;
import com.xlf.service.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Author xlf 2734029211@qq.com
 * @Date 2023/12/9 11:37
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class CommonUserVOService {


    @Autowired
    SysUserService sysUserService;


    /**
     * 获取用户信息
     * @param nickName
     * @return
     */
    public Set<Long> getUserIdsByNickName(String nickName){
        SysUser sysUser = new SysUser();
        sysUser.setNickName(nickName);
        Map<Long, UserVO> userInfoMap = getUserInfoMap(sysUser);
        return userInfoMap.keySet();
    }

    /**
     * 通过用户id获取用户信息
     * @param userIds
     * @return
     */
    public Map<Long, UserVO> getUserInfoMap(Set<String> userIds){
        return getUserInfoList(userIds).stream()
                .filter(item -> item.getUserId()!=null)
                .collect(Collectors.toMap(item -> item.getUserId(), item -> item));
    }

    /**
     * 通过用户id获取用户信息
     * @param userIds
     * @return
     */
    public List<UserVO> getUserInfoList(Set<String> userIds){

        List<SysUser> users = sysUserService.getUsersByIds(userIds);
        return Optional.ofNullable(BeanUtil.copyToList(users,UserVO.class)).orElse(new ArrayList<>());

    }

    /**
     * 获取用户信息
     * @param sysUser
     * @return
     */
    public List<UserVO> getUserInfoList(SysUser sysUser){

        return Optional.ofNullable(BeanUtil.copyToList(sysUserService.selectUserList(sysUser),UserVO.class))
                .orElse(new ArrayList<>());

    }

    /**
     * 获取用户信息
     * @param sysUser
     * @return
     */
    public Map<Long, UserVO> getUserInfoMap(SysUser sysUser){
        return getUserInfoList(sysUser).stream()
                .filter(item -> item.getUserId()!=null)
                .collect(Collectors.toMap(item -> item.getUserId(), item -> item));

    }


    /**
     * 获取用户信息
     * @return
     */
    public UserVO getUserInfo(Long id){
        return getUserInfo(id.toString());
    }

    public UserVO getUserInfoByUserName(String userName){
        return BeanUtil.toBean(sysUserService.selectUserByUserName(userName),UserVO.class);
    }
    /**
     * 获取用户信息
     * @return
     */
    public UserVO getUserInfo(String id){
        return BeanUtil.toBean(sysUserService.getUserById(Long.parseLong(id)),UserVO.class);
    }


    /**
     * 获取用户信息
     * @return
     */
    public void register(SysUser sysUser){
        sysUserService.register(sysUser);
    }

    /**
     * 更新用户信息
     * @param sysUser
     * @return
     */
    public boolean updateUserInfo(SysUser sysUser){
        return sysUserService.updateById(sysUser);
    }

    public List<SysUser> getUserInfo(LambdaQueryWrapper<SysUser> lambdaQueryWrapper){
        return sysUserService.list(lambdaQueryWrapper);
    }

}
